package com.zj.gencode;

import com.alibaba.fastjson.JSON;
import com.zj.gencode.beans.ClassField;
import com.zj.gencode.beans.PropField;
import com.zj.gencode.utils.VmClassUtil;
import com.zj.utils.FileUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author xi.yang
 * @create 2019-12-19 15:20
 **/
public class StructClassUtil {
    public static final String OUT_PATH = "C:\\Users\\d.cn\\Desktop\\Structs\\";
    public static final String READ_TXT_PATH = "D:\\C101\\SVNC101\\excel\\output_server_txt\\";
    private static final Set<String> ignores = new HashSet<>();
    static {
        ignores.add("1-cn-ip-latest.txt");
        ignores.add("2-ru-ip-latest.txt");
        ignores.add("CommonStruct.txt");
        ignores.add("WordFilterStruct.txt");
        ignores.add("HeroSourceStruct.txt");
    }

    public static void main(String[] args) {
        String[] files = FileUtil.getFileNames(READ_TXT_PATH);
        for (String file : files) {
            if (ignores.contains(file)) {
                continue;
            }
            List<String> strings = FileUtil.readLine(Paths.get(READ_TXT_PATH, file), Charset.forName("GBK"));
            String[] titles = strings.get(10).split("\t");
            String[] docs = strings.get(9).split("\t");
            System.out.println(JSON.toJSONString(titles));
            final String className = file.substring(0,file.indexOf("."));
            List<String> importClass = new ArrayList<>();
            importClass.add("com.downjoy.dgame.config.struct.BaseStruct");
            importClass.add(Getter.class.getName());
            importClass.add(EqualsAndHashCode.class.getName());
            List<String> annoClass = new ArrayList<>();
            annoClass.add(Getter.class.getSimpleName());
            annoClass.add("EqualsAndHashCode(callSuper = true)");
            List<PropField> propFields = new ArrayList<>();
            for (int i = 0; i < titles.length; i++) {
                final String title = titles[i];
                String doc = "";
                if (title.startsWith("_") || "iId".equalsIgnoreCase(title)) {
                    continue;
                }
                if (docs.length >= i) {
                    doc = docs[i];
                }
                List<String> annos = new ArrayList<>();
                String type = "String";
                if (title.startsWith("i")) {
                    type = "int";
                } else if (title.startsWith("t")) {
                    type = "List<Integer>";
                    importClass.add(List.class.getName());
                    annos.add("DollarData");
                    importClass.add("com.downjoy.dgame.config.antlr4.DollarData");
                }
                PropField propField = PropField.builder()
                        .fieldName(title.substring(1, 2).toLowerCase() + title.substring(2))
                        .fieldType(type)
                        .javaDoc(doc)
                        .annoClass(annos)
                        .build();
                propFields.add(propField);
            }
            ClassField.ClassFieldBuilder builder = ClassField.builder()
                    .packageName("com.downjoy.dgame.config.struct.structs")
                    .javaDoc(className)
                    .annoClass(annoClass)
                    .importClass(importClass)
                    .extendClass("BaseStruct")
                    .propFields(propFields)
                    .name(className);

            VmClassUtil.genClass(Paths.get(OUT_PATH,className+".java").toString(),builder.build());
        }
    }
}
